﻿using UnityEngine;
using System.Collections;

public class screenLimit : MonoBehaviour {

	public Vector2 posicion;
	private Vector3 posicionReal;
	private GameObject[] enemigos;

	// Use this for initialization
	void Start () {
		Camera camara;
		enemigos = GameObject.FindGameObjectsWithTag("EnemySI");
		camara = (GameObject.FindWithTag ("MainCamera").GetComponent ("Camera") as Camera);
		posicionReal = new Vector3(posicion.x, posicion.y, (camara.WorldToViewportPoint(GameObject.FindWithTag ("Player2").transform.position).z));
		transform.localScale = new Vector3(1, (camara.ViewportToWorldPoint (new Vector3 (posicionReal.x, 0, posicionReal.z)) - camara.ViewportToWorldPoint (new Vector3 (posicionReal.x, 0.5f, posicionReal.z))).magnitude*2, 1);
		if (posicion.x == 0) {
			transform.position = camara.ViewportToWorldPoint (posicionReal) - new Vector3((gameObject.GetComponent ("MeshRenderer") as MeshRenderer).bounds.extents.x-0.5f, 0, 0);
		}
		else {
			transform.position = camara.ViewportToWorldPoint (posicionReal) + new Vector3((gameObject.GetComponent ("MeshRenderer") as MeshRenderer).bounds.extents.x-0.7f, 0, 0);
		}
	}
	
	// Update is called once per frame
	void OnTriggerEnter(Collider  collision){
		if(collision.gameObject.layer != LayerMask.NameToLayer("Player")){
			for (int i = 0;i<enemigos.Length;i++){
				(enemigos[i].GetComponentInParent(typeof(enemySI)) as enemySI).setDown(true);
			}
		}
	}

	public void resetEnemies(){
		enemigos = GameObject.FindGameObjectsWithTag("EnemySI");
	}
}
